package com.zwh.maijiu.web;


import com.zwh.maijiu.model.EbProduct;
import com.zwh.maijiu.model.EbProductCategory;
import com.zwh.maijiu.service.EbProductCategoryService;
import com.zwh.maijiu.service.EbProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.Map;

/**
 * @Description: 用一句话描述
 * @Auther: 新梦想*陈超
 * @Date: 2020-12-09 08:14
 */
@Controller//迎宾证
@RequestMapping("/product")
public class EbProductController {

    @Autowired
    private EbProductService ebProductService;

    @Autowired
    private EbProductCategoryService ebProductCategoryService;

    @RequestMapping("/detail")
    public String detail(Model model, Integer epId){
        System.out.println("=====浏览器点击了某个商品======:epId=="+epId);
        System.out.println("测试");
        //1.调用service查询该商品的详细信息
        EbProduct productDetail = ebProductService.detail(epId);
        //1.1.调用service层查询商品分类信息
        Map<String, List<EbProductCategory>> categoryMap = ebProductCategoryService.selectBigAndSmallCate();
        //2.把数据绑定到model
        model.addAttribute("detail",productDetail);
        //2.1 将商品分类进行绑定
        model.addAttribute("categoryMap",categoryMap);
        //3.返回视图名称
        return "product-view";//templates/product-view.html
    }

    //product/cate_product_list/123
    @RequestMapping("/cate_product_list/{cateId}")
    public String productList(Model model,
                              @PathVariable("cateId") Integer cateId,
                              @RequestParam(value = "pSize", defaultValue = "1") Integer pSize,
                              @RequestParam(value = "pageSize", defaultValue = "12") Integer pageSize) {
        System.out.println("=======进入productList 这个方法=====cateId:" + cateId);
        //1.查询所有属于该二级分类的商品
        List<EbProduct> products = ebProductService.selectByCateId(cateId, pSize, pageSize);
        //1.1.调用service层查询商品分类信息
        Map<String, List<EbProductCategory>> categoryMap = ebProductCategoryService.selectBigAndSmallCate();
        //2.将所属分类商品数据绑定到model中

        model.addAttribute("products", products);
        //2.1 将商品分类进行绑定
        model.addAttribute("categoryMap", categoryMap);
        //返回视图
        return "product-list";
    }

    @RequestMapping("/search")
    public String search(Model model, String searchText) {
        System.out.println("==search== searchText=" + searchText);
        //1.调用service层的按searchText进行模糊查询
        List<EbProduct> productList = ebProductService.selectByCondition(searchText);
        //2.查询商品分类信息
        Map<String, List<EbProductCategory>> categoryMap = ebProductCategoryService.selectBigAndSmallCate();

        //3.将查询数来的数据绑定到model中
        model.addAttribute("products", productList);

        //4.传递查询条件
        model.addAttribute("searchText", searchText);

        //5.将商品分类进行绑定
        model.addAttribute("categoryMap", categoryMap);
        //6.返回视图：product-list.jsp
        return "product-list";

    }

}











